package com.dnjn.happiness.common.core.domain;



import java.util.Map;
import java.util.TreeMap;

public class KV<V> extends TreeMap<String, V> {

    private static final long serialVersionUID = 1L;

    public KV() {
        super();
    }

    public static <V> KV<V> create() {
        return new KV<V>();
    }

    public static KV<String> createSKV() {
        return new KV<String>();
    }

    public static KV<String> createSKV(String... data) {
        if (0 != data.length % 2) {
            throw new IllegalArgumentException("args's size must be even");
        }
        int i = 0;
        KV<String> kv = create();
        while (i < data.length) {
            kv.add(data[i], data[i + 1]);
            i += 2;
        }
        ;
        return kv;
    }


    public static KV<Object> by(String key, Object value) {
        return create().add(key, value);
    }

    public <T extends KV<V>> T add(String key, V value) {
        super.put(key, value);
        return self();
    }

    public <T extends KV<V>> T set(Map<String, V> map) {
        super.putAll(map);
        return self();
    }

    public <T extends KV<V>> T set(KV<V> KV) {
        super.putAll(KV);
        return self();
    }

    public <T extends KV<V>> T delete(Object key) {
        super.remove(key);
        return self();
    }

    public String getStr(String key) {
        V s = get(key);
        return s != null ? s.toString() : null;
    }

    public Integer getInt(String key) {
        V s = get(key);
        return Integer.valueOf(s.toString());
    }

   /* public String toJson() {
        return JsonUtils.toJsonString(this);
    }*/

    @SuppressWarnings("unchecked")
    private <T extends KV<V>> T self() {
        return (T) this;
    }
}